package org.jsola.hr.service.impl;

import org.jsola.core.Page;
import org.jsola.hr.common.ExampleKit;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dao.IEmpContactsUrgentDAO;
import org.jsola.hr.dto.EmpContactsUrgentAddDTO;
import org.jsola.hr.dto.EmpContactsUrgentUpdateDTO;
import org.jsola.hr.entity.EmpContactsUrgentDO;
import org.jsola.hr.query.EmpContactsUrgentQuery;
import org.jsola.hr.service.IEmpContactsUrgentService;
import org.jsola.hr.vo.EmpContactsUrgentListVO;
import org.jsola.hr.vo.EmpContactsUrgentVO;
import org.jsola.user.core.TokenUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 员工紧急联系人
 *
 * @author zhr
 */
@Service("hrEmpContactsUrgentServiceImpl")
public class EmpContactsUrgentServiceImpl implements IEmpContactsUrgentService {

    @Autowired
    private IEmpContactsUrgentDAO empContactsUrgentDAO;

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public EmpContactsUrgentVO save(EmpContactsUrgentAddDTO empContactsUrgentAddDTO, TokenUser tokenUser) {
        // 转DO
        EmpContactsUrgentDO empContactsUrgentDO = empContactsUrgentAddDTO.to(EmpContactsUrgentDO.class);
        // 保存
        empContactsUrgentDO = save(empContactsUrgentDO, tokenUser.getSiteId(), tokenUser.getUserId());
        return empContactsUrgentDO.to(EmpContactsUrgentVO.class);
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int update(EmpContactsUrgentUpdateDTO empContactsUrgentUpdateDTO, TokenUser tokenUser) {
        //转DO
        EmpContactsUrgentDO empContactsUrgentDO = empContactsUrgentUpdateDTO.to(EmpContactsUrgentDO.class);
        //根据主键更新，只更新非null值
        return updateByIdSelective(empContactsUrgentDO, tokenUser.getSiteId(), tokenUser.getUserId());
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int deleteByIds(TokenUser tokenUser, String... empContactsUrgentIds) {
        return logicalDeleteByIds(tokenUser.getSiteId(), tokenUser.getUserId(), (Object[]) empContactsUrgentIds);
    }


    @Override
    public EmpContactsUrgentVO selectById(String empContactsUrgentId, String siteId) {
        EmpContactsUrgentDO empContactsUrgentDO = selectDOById(empContactsUrgentId, siteId);
        if (empContactsUrgentDO == null) {
            return null;
        }
        return empContactsUrgentDO.to(EmpContactsUrgentVO.class);
    }

    @Override
    public List<EmpContactsUrgentListVO> select(EmpContactsUrgentQuery empContactsUrgentQuery, String siteId) {
        List<EmpContactsUrgentDO> empContactsUrgentDOList = selectDO(empContactsUrgentQuery, siteId);
        if(CollectionUtils.isEmpty(empContactsUrgentDOList)) {
            return empContactsUrgentDOList == null ? null : new ArrayList<>();
        }
        return empContactsUrgentDOList.stream()
                .map(empContactsUrgentDO -> empContactsUrgentDO.to(EmpContactsUrgentListVO.class))
                .collect(Collectors.toList());
    }

    @Override
    public int selectCount(EmpContactsUrgentQuery empContactsUrgentQuery, String siteId) {
        Example example = buildExample(empContactsUrgentQuery,siteId);
        return empContactsUrgentDAO.selectCountByExample(example);
    }

    @Override
    public Page<EmpContactsUrgentListVO> selectPage(EmpContactsUrgentQuery empContactsUrgentQuery, String siteId) {
        Example example = buildExample(empContactsUrgentQuery,siteId);
        Page<EmpContactsUrgentDO> page = empContactsUrgentDAO.selectPageByExample(example,
                empContactsUrgentQuery.getPageNo(),
                empContactsUrgentQuery.getPageSize());

        return page.to(EmpContactsUrgentListVO.class);
    }

    @Override
    public EmpContactsUrgentDO selectDOById(String empContactsUrgentId, String siteId) {
        return listById(empContactsUrgentId, siteId);
    }

    @Override
    public List<EmpContactsUrgentDO> selectDO(EmpContactsUrgentQuery empContactsUrgentQuery, String siteId) {
        Example example = buildExample(empContactsUrgentQuery,siteId);
        return empContactsUrgentDAO.selectByExample(example);
    }


    /**
     * 根据查询参数，构建example

     * @param empContactsUrgentQuery 查询参数
     * @param siteId 所属站点id
     * @return example
     */
    private Example buildExample(EmpContactsUrgentQuery empContactsUrgentQuery, String siteId) {
        Example example = new Example(EmpContactsUrgentDO.class);
        example.and()
                .andEqualTo("valid", true)
                .andEqualTo("siteId", siteId);
        if (empContactsUrgentQuery.getEmpId() != null) {
            example.and().andEqualTo("empId", empContactsUrgentQuery.getEmpId());
        }
        if (empContactsUrgentQuery.getName() != null) {
            example.and().andEqualTo("name", empContactsUrgentQuery.getName());
        }
        if (empContactsUrgentQuery.getRelation() != null) {
            example.and().andEqualTo("relation", empContactsUrgentQuery.getRelation());
        }
        if (empContactsUrgentQuery.getPhone() != null) {
            example.and().andEqualTo("phone", empContactsUrgentQuery.getPhone());
        }
        if (empContactsUrgentQuery.getAddress() != null) {
            example.and().andEqualTo("address", empContactsUrgentQuery.getAddress());
        }
        if (empContactsUrgentQuery.getCompanyId() != null) {
            example.and().andEqualTo("companyId", empContactsUrgentQuery.getCompanyId());
        }
        // 排序
        ExampleKit.setExampleOrder(example,empContactsUrgentQuery.getOrders());
        return example;
    }
}




